package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

@RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> users=userService.findAll();
        model.addAttribute("user",users);
        return "show";
    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }
    @RequestMapping("/add")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll";
    }
    @RequestMapping("/toupdate")
    public String toupdate(Integer id,Model model){
        User user=userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }
    @RequestMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:findAll";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:findAll";
    }

}
